package com.md.value.html;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 刘骄阳
 * 2024-05-20 18:42
 */
@AllArgsConstructor
@Getter
public enum TextAlign {
  /**
   * 左对齐
   */
  LEFT("left"),
  /**
   * 右对齐
   */
  RIGHT("right"),
  /**
   * 居中对齐
   */
  CENTER("center"),
  /**
   * 两端对齐
   */
  JUSTIFY("justify");
  static final Map<String, TextAlign> ENUM_MAP_STRING = Arrays.stream(TextAlign.values())
    .collect(Collectors.toMap(TextAlign::getValue, e -> e));
  private final String value;

  public static TextAlign convertToHtml(final ParagraphAlignment paragraphAlignment) {
    switch (paragraphAlignment) {
      case CENTER:
        return TextAlign.CENTER;
      case RIGHT:
        return TextAlign.RIGHT;
      case BOTH:
        return TextAlign.JUSTIFY;
      default:
        return TextAlign.LEFT;
    }
  }

  public static TextAlign of(final String styleName) {
    return Optional.ofNullable(ENUM_MAP_STRING.get(styleName)).orElse(TextAlign.LEFT);
  }

  /**
   * 将样式转为excel
   *
   * @return 样式
   */
  public HorizontalAlignment convertToExcel() {
    switch (this) {
      case CENTER:
        return HorizontalAlignment.CENTER;
      case RIGHT:
        return HorizontalAlignment.RIGHT;
      case JUSTIFY:
        return HorizontalAlignment.JUSTIFY;
      default:
        return HorizontalAlignment.LEFT;
    }
  }
}
